﻿@{
    ViewData["Title"] = "任务调度管理";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
            <li class="layui-this">作业信息</li>
            <li>脚本代码</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form" action="" lay-filter="val-filter">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">作业编号</label>
                            <div class="layui-input-inline">
                                <input type="hidden" name="id" value="0">
                                <input type="text" name="jobId" autocomplete="off" lay-verify="required" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">组名称</label>
                            <div class="layui-input-inline">
                                <input type="text" value="default" name="groupName" autocomplete="off" lay-verify="required" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">创建类型</label>
                        <div class="layui-input-block">
                            <input type="radio" name="createType" value="0" title="内置" lay-filter="createType">
                            <input type="radio" name="createType" value="1" title="脚本" lay-filter="createType">
                            <input type="radio" name="createType" value="2" title="Http请求" lay-filter="createType">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">执行方式</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="concurrent" value="true" title="并行" checked>
                                <input type="radio" name="concurrent" value="false" title="串行">
                            </div>
                        </div>
                        <div class="layui-inline script">
                            <label class="layui-form-label">扫描触发器</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="includeAnnotations" value="true" title="是" checked>
                                <input type="radio" name="includeAnnotations" value="false" title="否">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">描述信息</label>
                        <div class="layui-input-block">
                            <input type="text" name="description" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item script">
                        <label class="layui-form-label">额外数据</label>
                        <div class="layui-input-block">
                            <textarea name="properties" class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item http">
                        <label class="layui-form-label">请求地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="requestUri" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item http">
                        <label class="layui-form-label">请求方法</label>
                        <div class="layui-input-block">
                            <input type="radio" name="httpMethod" value="get" title="GET" checked>
                            <input type="radio" name="httpMethod" value="post" title="POST">
                            <input type="radio" name="httpMethod" value="put" title="PUT">
                            <input type="radio" name="httpMethod" value="delete" title="DELETE">
                        </div>
                    </div>
                    <div class="layui-form-item http">
                        <label class="layui-form-label">请求报文体</label>
                        <div class="layui-input-block">
                            <textarea name="body" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <pre id="editor" style="height: calc(100vh - 80px)">
#region using

using Furion;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Furion.Schedule;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Data;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;

#endregion

namespace Admin.NET.Core;

/// <summary>
/// 动态作业任务
/// </summary>
[JobDetail("你的作业编号")]
public class DynamicJob : IJob
{
    private readonly IServiceProvider _serviceProvider;

    public DynamicJob(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        using var serviceScope = _serviceProvider.CreateScope();

        // 获取用户仓储
        // var rep = serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysUser>>();

        // 请求网址
        // var result = await "http://www.baidu.com".GetAsStringAsync();
        // Console.WriteLine(result);

        // 日志
        // Log.Information("日志消息");
    }
}
                </pre>
            </div>
        </div>
    </div>
</div>

@section scripts {
    <script src="/component/libs/ace/ace.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        var app = {};
        //
        var editor = ace.edit("editor");
        editor.session.setMode("ace/mode/csharp");

        layui.use(['http', 'toast'], function () {
            var form = layui.form, $ = layui.$,
                http = layui.http, toast = layui.toast;

            app.save = function () {
                form.submit('val-filter', function (data) {
                    var postData = data.field
                    //
                    if (postData.createType == 1)
                        postData.scriptCode = editor.getValue();
                    else
                        postData.scriptCode = '';

                    // Http请求创建类型
                    if (postData.createType == 2) {
                        // 将 httpJobMessage 重新封装，按后端 HttpJob 序列化要求，字段要大写开头
                        // HttpJob 约定读取属性为“HttpJob”的值
                        var httpJobPropValue = JSON.stringify({
                            RequestUri: postData.requestUri,
                            HttpMethod: { Method: postData.httpMethod },
                            Body: postData.body,
                            ClientName: 'HttpJob',
                            EnsureSuccessStatusCode: true,
                        });
                        var prop = { HttpJob: httpJobPropValue };
                        postData.properties = JSON.stringify(prop);
                    }

                    //
                    var url = postData.id == '0' ? '/api/sysJob/AddJobDetail' : '/api/sysJob/UpdateJobDetail'
                    http.post(url, postData).then((res) => {
                        if (res.code == 200) {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload('lsttable');
                            toast.success({ message: '保存成功！' });
                        }
                        else {
                            layer.alert('保存失败，错误信息:' + res.message, { icon: 2 });
                        }
                    });

                    return false;
                });
            }

            //
            app.init = function () {
                var data = app.data || {};
                //HTTP请求初始化处理
                if (data.createType == 2) {
                    var value = JSON.parse(JSON.parse(data.properties).HttpJob);
                    data.requestUri = value.RequestUri;
                    data.httpMethod = value.HttpMethod.Method;
                    data.body = value.Body;
                }
                form.val('val-filter', data);
                //
                if (data.scriptCode != undefined) editor.setValue(data.scriptCode);
                $('.script').hide();
                form.on('radio(createType)', function (data) {
                    if (data.elem.value == 1) {
                        $('.http').hide();
                        $('.script').show();
                    }
                    else {
                        $('.http').show();
                        $('.script').hide();
                    }
                });
                if (data.createType == undefined) {
                    $('input[name=createType][value=0]').prop('disabled', true);
                    $('input[name=createType][value=2]').prop('checked', true);
                }
                else {
                    $('input[name=createType]').prop('disabled', true);
                }
                form.render('radio');
            }();
        })
    </script>
}
